/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package braipetro.interfaces.regras;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author manasses
 */
public class Metodo {
    private Method mtd;
    private Class c;
    private String nome;
    private String pacote;
    private String classe;
    private List<Class> params = new ArrayList<Class>();
    
    public Metodo(String pacote, String classe, String nome) {
        this.pacote = pacote;
        this.classe = classe;
        this.nome = nome;
        
        loadMethod();
    }
    
    private void loadMethod() {
        
        try {

            c = Class.forName(pacote + "." + classe);
            
            Class[] prm = new Class[params.size()];

            for (int i = 0; i < params.size(); i++)
            {
                prm[i] = params.get(i);
            }
            
            mtd = c.getMethod(nome, prm);
            
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Criterio.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoSuchMethodException ex) {
            Logger.getLogger(Criterio.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SecurityException ex) {
            Logger.getLogger(Criterio.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalArgumentException ex) {
            Logger.getLogger(Metodo.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public String getPacote() {
        return pacote;
    }

    public void setPacote(String pacote) {
        this.pacote = pacote;
    }

    public String getClasse() {
        return classe;
    }

    public void setClasse(String classe) {
        this.classe = classe;
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public List<Class> getParams() {
        return params;
    }

    public void setParams(List<Class> params) {
        this.params = params;
    }
    
    public Object execute() {
        Object res = null;
       
        try {
            
            res = mtd.invoke(null, null);
            
        } catch (SecurityException ex) {
            Logger.getLogger(Criterio.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(Metodo.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalArgumentException ex) {
            Logger.getLogger(Metodo.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvocationTargetException ex) {
            Logger.getLogger(Metodo.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return res;
    }
}
